package SecondeStage.Collection.Map;

import java.util.Comparator;
import java.util.TreeMap;

@SuppressWarnings({"all"})
public class TreeMap_ {
    public static void main(String[] args) {
        // TreeMap实现了Map接口
        // 使用无参构造器创建TreeMap对象时，是无序的
        TreeMap treeMap =new TreeMap();
        treeMap.put("mike","麦克");
        treeMap.put("jack","杰克");
        treeMap.put("tom","汤姆");
        treeMap.put("lucy","露西");
        System.out.println(treeMap);

        TreeMap treeMap2 =new TreeMap(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                // 调用String的compareTo方法进行字符串大小比较
                //return ((String)o1).compareTo((String)o2);
                // 按照字符串长度大小排序
                return ((String)o1).length()-((String)o2).length();
            }
        });

        //由于是按照字符串长度，后续的jack和lucy长度和mike一样都是4，所以不能添加，但value的值被替换了
        treeMap2.put("mike","麦克");
        treeMap2.put("jack","杰克");
        treeMap2.put("tom","汤姆");
        treeMap2.put("lucy","露西");
        System.out.println(treeMap2);
    }

}
